package com.tm.fiverole.service.impl;

import com.tm.fiverole.entity.MenuEntity;
import com.tm.fiverole.entity.RoleEntity;
import com.tm.fiverole.entity.RoleMenuEntity;
import com.tm.fiverole.entity.UserEntity;
import com.tm.fiverole.mapper.MenuMapper;
import com.tm.fiverole.mapper.RoleMapper;
import com.tm.fiverole.mapper.RoleMenuMapper;
import com.tm.fiverole.mapper.UserRoleMapper;
import com.tm.fiverole.result.Result;
import com.tm.fiverole.result.ResultEnum;
import com.tm.fiverole.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangSang
 * @create 2022-08-18 19:00
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper mapper;

    /***
     * 查询角色
     * @param id
     * @return
     */
    @Override
    public Result queryRoleId(Integer id) {
        //查询角色的权限
        List<RoleMenuEntity> RoleMenuList = roleMenuMapper.queryRoleId(id);
        List<Long> collect = RoleMenuList.stream().map(RoleMenuEntity::getMenuId).collect(Collectors.toList());
        List<MenuEntity>    roleMenuEntityList=  mapper.queryMenu(collect);

        return Result.SUCCESS(roleMenuEntityList);
    }

    @Override
    @Transactional
    public Result addRole(RoleEntity roleEntity) {
        roleEntity.setCreateTime(new Date());
        roleEntity.setCreateUser("毛毛");
        roleMapper.addRole(roleEntity);
        List<MenuEntity> menuEntityList = roleEntity.getMenuEntityList();
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
        for (int i = 0; i <menuEntityList.size() ; i++) {

            MenuEntity menuEntity = menuEntityList.get(i);
            roleMenuEntity.setRoleId(roleEntity.getId());
            roleMenuEntity.setMenuId(menuEntity.getId());
            roleMenuEntity.setCreateTime(new Date());
            roleMenuEntity.setCreateUser("毛毛");
            roleMenuMapper.addRoleMenu(roleMenuEntity);

        }

        return Result.SUCCESS();
    }

    @Override
    public Result updateRole(RoleEntity roleEntity) {
        roleEntity.setUpdateTime(new Date());
        roleEntity.setUpdateUser("毛毛");
        roleMapper.updateRole(roleEntity);
        roleMenuMapper.deleteRole(roleEntity.getId());
        List<MenuEntity> menuEntityList = roleEntity.getMenuEntityList();
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
        for (int i = 0; i <menuEntityList.size() ; i++) {

            roleMenuEntity.setRoleId(roleEntity.getId());
            roleMenuEntity.setMenuId(menuEntityList.get(i).getId());
            roleMenuEntity.setCreateUser("病病");
            roleMenuEntity.setCreateTime(new Date());
            roleMenuMapper.addRoleMenu(roleMenuEntity);

        }
        return Result.SUCCESS();
    }

    @Override
    @Transactional
    public Result deleteRole(Integer id) {

      List<UserEntity>   userList= userRoleMapper.quertyId(id);
        if(userList.size()>0 || userList !=null){

            return  Result.ERROR(ResultEnum.DELETE_ERROR);
        }
        roleMapper.deleteRole(id);

        return Result.ERROR();
    }
}
